package com.example.demo.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Procurement;
import com.example.demo.entity.User;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.ProcurementMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/4/17
 */

@RestController
@RequestMapping("/procurement")
public class ProcurementController extends BaseController{
    
        @Resource
        ProcurementMapper procurementMapper;
    
    
        @Resource
        OrderMapper OrderMapper;
        
        @PostMapping
        public Result<?> save(@RequestBody Procurement procurement) {
            procurementMapper.insert(procurement);
            return Result.success();
        }
        
        @PutMapping
        public Result<?> update(@RequestBody Procurement procurement) {
            procurementMapper.updateById(procurement);
            return Result.success();
        }
        
        @DeleteMapping("/{id}")
        public Result<?> delete(@PathVariable Integer id) {
            procurementMapper.deleteById(id);
            return Result.success();
        }
        
        @PostMapping("/deleteBatch")
        public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
            procurementMapper.deleteBatchIds(ids);
            return Result.success();
        }
        
        @GetMapping("/{id}")
        public Result<?> getById(@PathVariable Integer id) {
            return Result.success(procurementMapper.selectById(id));
        }
        
    
        @GetMapping
        public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                @RequestParam(defaultValue = "10") Integer pageSize,
                @RequestParam(defaultValue = "") String search,
                @RequestParam(defaultValue = "") String username) {
            LambdaQueryWrapper<Procurement> wrapper = Wrappers.<Procurement>lambdaQuery();
            if (StrUtil.isNotBlank(search)) {
                wrapper.like(Procurement::getName, search);
            }
            wrapper.eq(Procurement::getUsername,username);
            Page<Procurement> ProcurementPage = procurementMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
            return Result.success(ProcurementPage);
        }
}
